﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"Snake.h"

//设置光标的坐标
void SetPos(short x, short y)
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

//打印欢迎界面
WelcomeToGame() {
	SetPos(35, 13);
	wprintf(L"欢迎来到贪吃蛇小游戏");
	SetPos(36, 17);
	system("pause");

	system("cls");

	SetPos(24, 13);
	wprintf(L"按↑ . ↓ . ← . → 来控制蛇的移动，F3加速 ，F4减速");
	SetPos(35, 14);
	wprintf(L"加速能够得到更高的分数");
	SetPos(36, 17);
	system("pause");

	system("cls");

}

//绘制地图
CreateMap() {
	//上
	for (int i = 0; i < 29; i++) {
		wprintf(L"%lc", WALL);
	}

	//下
	SetPos(0,26);
	for (int i = 0; i < 29; i++) {
		wprintf(L"%lc", WALL);
	}

	//左
	for (int i = 1; i <= 25; i++) {
		SetPos(0,i);
		wprintf(L"%lc", WALL);
	}

	//右
	for (int i = 1; i <= 25; i++) {
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}

	/*getchar();*/
}

//创建一条贪吃蛇
void InitSnake(pSnake ps) {

	pSnakeNode cur = NULL; //创建蛇身节点指针

	for (int i = 0; i < 5; i++) 
	{

		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->next = NULL;
		cur->x = pos_x + i * 2;  //往x轴，走一格
		cur->y = pos_y;

		//使用头插法把节点依次插入
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;  //空链表的情况
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}

	//打印蛇
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BOYD);
		cur = cur->next;
	}

	//初始化蛇的其他属性
	ps->_dir = RIGHT;
	ps->_food_weight = 10;
	ps->_score = 0;
	ps->_sleep_time = 200;
	ps->_status = OK;

	/*getchar();*/
}

//创建食物
void CreatFood(pSnake ps) {
	int x = 0;
	int y = 0;

again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);    //x不是2的倍数，继续生成x的随机数

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if ((x == cur->x) && (y == cur->y))
		{
			goto again;
		}
		cur = cur->next;
	}

	//创建食物节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood()::malloc()");
	}

	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;

	SetPos(x, y);
	wprintf(L"%lc", FOOD);

	ps->_pFood = pFood;
	/*getchar();*/
}

//1.开始游戏
void GameStart(pSnake ps) {
	//1. 设置窗口大小名字，以及隐藏光标
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	//影藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态

	//2.打印欢迎界面
	WelcomeToGame();

	//3.绘制地图
	CreateMap();

	//4.创建一条贪吃蛇
	InitSnake(ps);

	//5.创建食物
	CreatFood(ps);

}
//----------------------------------------------------------
PrintfHelpInfo() {

	SetPos(62, 10);
	wprintf(L"%ls", L"不能穿墙，不能撞到自己");
	SetPos(62, 11);
	wprintf(L"%ls", L"按↑ . ↓ . ← . → 来控制蛇的移动");
	SetPos(62, 12);
	wprintf(L"%ls", L"F3加速 ，F4减速");
	SetPos(62, 13);
	wprintf(L"%ls", L"按Esc退出游戏，按空格暂停游戏");

	SetPos(62, 16);
	wprintf(L"%ls", L"———玖伍出品———");
}

//暂停游戏
Pause() {

	while (1) {
		Sleep(200);
		if(KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

//判断下一个位置是不是食物
int NextIsFood(pSnakeNode pn, pSnake ps) {

	//若是食物返回非零值，不是食物返回假
	return((pn->x == ps->_pFood->x) && (pn->y == ps->_pFood->y));
	
}

//吃掉食物
void EatFood(pSnakeNode pn, pSnake ps) {

	//头插法，把食物插入链表头部
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;

	//释放下一个节点
	free(pn);
	pn = NULL;

	//打印蛇,这里已经把食物覆盖了
	pSnakeNode cur = ps->_pSnake;
	while(cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BOYD);
		cur = cur->next;
	}

	//吃了食物加总分数
	ps->_score += ps->_food_weight;

	//再次创建食物
	CreatFood(ps);

}

//不是食物走一步
void NoFood(pSnakeNode pn, pSnake ps) {

	//将下一个位置头插值链表中
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;

	//打印新蛇，不用打印旧蛇的最后一个节点
	pSnakeNode cur = ps->_pSnake;
	do                                                     //这里不知道为什么while循环用不了，do while循环蛇就可以动了
	{
		SetPos(cur->x, cur->y);                           
		wprintf(L"%lc", BOYD);
		cur = cur->next;
	}while (cur->next->next != NULL);

	//覆盖旧蛇的最后一个节点
	SetPos(cur->next->x,cur->next->y);
	printf("  ");

	//释放旧蛇的最后一个节点
	free(cur->next);
	cur->next = NULL;

	//将新蛇的最后一个节点的next置为空
	cur->next = NULL;
}

//3.检测蛇是否撞到墙
void KillByWall(pSnake ps) {
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 || ps->_pSnake->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}

//4.检测蛇是否撞到自己
void KillBySelf(pSnake ps) {
	pSnakeNode cur = ps->_pSnake->next;

	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}

//蛇走一步的过程
void SnakeMove(pSnake ps) {
	//蛇头的下一个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}

	//蛇头的方向决定往哪里走
	switch (ps->_dir) 
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y -1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}

	//判断下一个节点是不是食物
	//若是食物返回非零值，不是食物返回假
	if (NextIsFood(pNextNode,ps)) {
		//1.是食物吃掉
		EatFood(pNextNode, ps);
	}
	else
	{
		//2.不是食物走一步
		NoFood(pNextNode, ps);
	}

	//3.检测蛇是否撞到墙
	KillByWall(ps);

	//4.检测蛇是否撞到自己
	KillBySelf(ps);

}

#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )
//2.游戏运行
void GameRun(pSnake ps) {

	//打印帮助信息
	PrintfHelpInfo();
	
	//检测按键情况,和蛇的状态
	do
	{
		SetPos(64,7);
		printf("总得分：%d ", ps->_score);
		SetPos(64, 8);
		printf("每个食物得分：%2d分", ps->_food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			//向上走的蛇方向不能是往下
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			//向下走的蛇方向不能是往上
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			//向左走的蛇方向不能是往右
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			//向右走的蛇方向不能是往左
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_food_weight > 2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}

		//蛇走一步的过程
		SnakeMove(ps);
		Sleep(ps->_sleep_time);

	} while (ps->_status == OK);

}
//-----------------------------------------------------------------
//3.游戏结束
void GameOvre(pSnake ps) {

	SetPos(24,12);
	switch (ps->_status) 
	{
	case END_NORMAL:
		printf("您主动结束了游戏");
		break;
	case KILL_BY_WALL:
		printf("您撞上了墙，结束了游戏");
		break;
	case KILL_BY_SELF:
		printf("您撞上了自己，结束了游戏");
		break;
	}

	//释放链表
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}

}